package com.king.wms.service;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.king.wms.mapper.LocMapper;
import com.king.wms.mapper.OrdersMapper;
import com.king.wms.mapper.SkuMapper;
import com.king.wms.mapper.StorerMapper;
import com.king.wms.pojo.Loc;
import com.king.wms.pojo.Orders;
import com.king.wms.pojo.Sku;
import com.king.wms.pojo.Storer;

@Service
@Transactional
public class OrdersService {
	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private SkuMapper skuMapper;
	@Autowired
	private LocMapper locMapper;

	public List<Orders> findAll() {
		List<Orders> list = ordersMapper.findAll();
		return list;
	}

	public void delete(Integer ordersId) {
		ordersMapper.deleteByPrimaryKey(ordersId);
	}
	public String insert(Orders orders) {
		
		Sku sku = skuMapper.findSkuBySku_id(orders.getSku_id());
		System.out.println("-========================-sku_id"+orders.getSku_id());
		orders.setExpenses(( sku.getRetailPrice() - sku.getPurchasePrice() ) * orders.getQuantity() );
		
		Loc loc = locMapper.selectByPrimaryKey(orders.getLoc_id());
		//Loc loc_find = locMapper.findSkuByloc_id(orders.getLoc_id())//查找该库中是否有商品
		Loc  loc_find = locMapper.findLocBuySku_idAndLoc_id(orders.getSku_id(), orders.getLoc_id());
		
		Integer quantity = orders.getQuantity();
		Integer remain = loc.getStackremianed();
		Integer used = loc.getStackuesd();
		
		if( loc_find == null  ){
			
			return "该  "+loc.getRegion()+" 区域没有存放 "+sku.getName()+" 商品,请换区查看";
			
		}else if( quantity > used){
			return "该  "+loc.getRegion()+" 区域存放仅 "+used+" 个单位的商品,需要出库  "+quantity+" 个单元的商品,库存不足,请重新操作";
		}else{
			remain = remain + quantity;
			used = used - quantity;
			loc.setStackremianed(remain);
			loc.setStackuesd(used);
			ordersMapper.insert(orders);
			return "success";
		}
		
	}

	public List<Orders> findOut() {
		// TODO Auto-generated method stub
		return ordersMapper.findOut();
	}

	public List<Orders> findNotOut() {
		// TODO Auto-generated method stub
		return ordersMapper.findNotOut();
	}

	public Orders findReceiptByPrimaryKey(Integer orders_id) {
		// TODO Auto-generated method stub
		return ordersMapper.findReceiptByPrimaryKey(orders_id);
	}

	public void updateStatus(Integer orders_id) {
		// TODO Auto-generated method stub
		ordersMapper.updateStatus(orders_id);
	}

	public String update(Orders orders) {
		
		Sku sku = skuMapper.findSkuBySku_id(orders.getSku_id());
		orders.setExpenses(( sku.getRetailPrice() - sku.getPurchasePrice() ) * orders.getQuantity() );
		
		
		Loc loc = locMapper.selectByPrimaryKey(orders.getLoc_id());
		Loc  loc_find = locMapper.findLocBuySku_idAndLoc_id(orders.getSku_id(), orders.getLoc_id());
		
		Integer quantity = orders.getQuantity();
		Integer remain = loc.getStackremianed();
		Integer used = loc.getStackuesd();
		
		if( loc_find == null  ){
			
			return "该  "+loc.getRegion()+" 区域没有存放 "+sku.getName()+" 商品,请换区查看";
			
		}else if( quantity > used){
			return "该  "+loc.getRegion()+" 区域存放仅 "+used+" 个单位的商品,需要出库  "+quantity+" 个单元的商品,库存不足,请重新操作";
		}else{
			remain = remain + quantity;
			used = used - quantity;
			loc.setStackremianed(remain);
			loc.setStackuesd(used);
			ordersMapper.update(orders);
			return "success";
		}
	}

}
